package com.kystar.muxplayer.ui;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import com.kystar.muxplayer.BaseApp;
import com.kystar.muxplayer.R;
import com.kystar.muxplayer.base.SpinnerAdapter;
import com.kystar.muxplayer.model.PGMConfig;
import com.kystar.muxplayer.model.PVWConfig;
import com.kystar.muxplayer.protocol.code.get.CG88_CutTakeEffect;
import com.kystar.muxplayer.protocol.code.set.CS0A_CutTakeEffect;
import com.kystar.muxplayer.protocol.code.set.CS14_CutTake;

public class ButtonSetting implements OnClickListener, OnItemSelectedListener {

    private Spinner spinnerType;
    private Spinner spinnerDuration;

    public ButtonSetting(Context context, View view) {
        view.findViewById(R.id.pvw_effect_cut).setOnClickListener(this);
        view.findViewById(R.id.pvw_effect_take).setOnClickListener(this);
        spinnerType = view.findViewById(R.id.pvw_effect_type);
        spinnerType.setAdapter(new SpinnerAdapter(context, R.layout.spinner_pvw_effect,
            R.layout.spinner_source_dropdown, R.array.take_effect));
        spinnerType.setOnItemSelectedListener(this);

        spinnerDuration = view.findViewById(R.id.pvw_effect_duration);
        spinnerDuration.setAdapter(new SpinnerAdapter(context, R.layout.spinner_pvw_effect,
            R.layout.spinner_source_dropdown, R.array.take_duration));
        spinnerDuration.setOnItemSelectedListener(this);

//        BaseApp.send(CG88_CutTakeEffect.switchLayer()).subscribe(cmd -> {
//            int[] r = cmd.getConfig();
//            spinnerType.setSelection(r[0]);
//            spinnerDuration.setSelection(r[1]);
//        });
        PVWConfig pvwConfig = BaseApp.getSettings().getPVWConfig();
        spinnerType.setSelection(pvwConfig.getEffectType());
        spinnerDuration.setSelection(pvwConfig.getEffectDuration());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pvw_effect_cut:
                BaseApp.send(CS14_CutTake.crateCut()).subscribe(cmd -> {
                    BaseApp.updateLayers();
                });
                break;
            case R.id.pvw_effect_take:
                BaseApp.send(CS14_CutTake.crateTake()).subscribe(cmd -> {
                    BaseApp.updateLayers();
                });
                break;
        }
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        BaseApp.send(CS0A_CutTakeEffect.create(spinnerType.getSelectedItemPosition(),
            spinnerDuration.getSelectedItemPosition())).subscribe(cmd -> {

        }, e -> {
            e.printStackTrace();
        });
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
